import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-10-04
 * Time:12:12
 */
public class CardDemo {
    public static final String[] suits = {"♥","♠","♣","♦"};

    //买一副牌
    public List<Card> buyCard() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                int rank = i;
                String suit = suits[j];
                Card card = new Card(suit,rank);
                cardList.add(card);
            }
        }
        return cardList;
    }

    //洗牌
    public void shuffle(List<Card> cardList) {
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0; i--) {
            //random.nextInt()是Java中Random类的一个方法，用于生成一个随机的整数值
            //nextInt()：生成一个随机的 int 值，范围是所有可能的 int 值
            //nextInt(int bound)：生成一个在 [0, bound) 范围内的随机 int 值，包含 0 但不包含 bound
            int index = random.nextInt(i);//random.nextInt(51),会生成 0~50之间的随机数
            swap(cardList,i,index);
        }
    }
    private void swap(List<Card> cardList,int i,int j) {
        /*
        Card tmp = cardList[i];
        cardList[i] = cardList[j];
        cardList[j] = tmp;
        */
        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    //三个人，每个人轮流揭5张牌
    public List<List<Card>> play(List<Card> cardList) {
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand0);
        hand.add(hand1);
        hand.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //每个人拿完一张牌后，最上面的那种牌就删除掉
                Card card = cardList.remove(0);
                //将对应的牌 放到对应的人的手里
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
